middleware.js ➔ ???   B
last analyzed

Complexity

Conditions 1
Paths 0

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 0
dl 0
loc 29
rs 8.8571
nop 1
1
import { ACTION_PREFIX, ACTION_TYPES, HISTORY_METHODS} from './constants';
2
import { parsePath } from 'history';
3
4
export default ({ history, routeParser }) => ({ dispatch, getState }) => next => action => {
0 ignored issues
show
Unused Code introduced by
The parameter getState is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter dispatch is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
5
6
    if (action.type.indexOf(ACTION_PREFIX) === 0 && action.type !== ACTION_TYPES.LOCATION_CHANGED) {
7
8
        if (action.type === ACTION_TYPES.GO_TO_ROUTE) {
9
            action.type = ACTION_TYPES.PUSH;
10
            action.payload = routeParser(action.payload);
11
        }
12
13
        if ([ACTION_TYPES.PUSH, ACTION_TYPES.REPLACE].includes(action.type)) {
14
15
            action.payload = typeof action.payload === 'string' ? parsePath(action.payload) : action.payload;
16
17
            const sameLocation = history.location.pathname === action.payload.pathname
18
                              && history.location.search === action.payload.search
19
                              && history.location.hash === action.payload.hash;
20
21
            action.type = sameLocation ? ACTION_TYPES.REPLACE : action.type;
22
        }
23
24
        if (HISTORY_METHODS[action.type]) {
25
            history[HISTORY_METHODS[action.type]](action.payload);
26
        }
27
28
        return;
29
    }
30
31
    return next(action); // eslint-disable-line consistent-return
32
};